package com.leyou.order.controller;

import com.leyou.order.dto.CartToOrderDTO;
import com.leyou.order.dto.OrderDTO;
import com.leyou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/order")
    public ResponseEntity<Long> createOrder(@RequestBody CartToOrderDTO order){
        Long orderId = orderService.createOrder(order);
        return ResponseEntity.ok(orderId);
    }

    @GetMapping("/order/{id}")
    public ResponseEntity<OrderDTO> findOrderById(@PathVariable("id") Long id){
        OrderDTO orderDTO = orderService.findOrderById(id);
        return ResponseEntity.ok(orderDTO);
    }

    @GetMapping("/order/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long id){
        String payUrl = orderService.getPayUrl(id);
        return ResponseEntity.ok(payUrl);
    }

    @GetMapping("/order/state/{id}")
    public ResponseEntity<Integer> queryStatus(@PathVariable("id") Long id){
        Integer status = orderService.queryStatus(id);
        return ResponseEntity.ok(status);
    }

    /**
     * produces="application/xml"
     * 指定响应结果的数据类型
     * @param resp
     * @return
     */
    @PostMapping(value = "/pay/wx/notify", produces="application/xml")
    public Map<String, String> notifyWx(@RequestBody Map<String, String> resp){
        //验证支付是否成功
        orderService.checkNotifyResp(resp);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("return_code","SUCCESS");
        resultMap.put("return_msg","OK");
        return resultMap;
    }

}
